package dao.impl;

import dao.BaseDao;
import dao.OrdersDao;
import vo.OrdersVo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl extends BaseDao implements OrdersDao {

    /**
     * 分页查询
     * @param pageNo      记录页数
     * @param pageSize    展示的数据数量
     * @param sql
     * @return
     */
    @Override
    public List<OrdersVo> selectAll(int pageNo, int pageSize, String sql) {
        List<OrdersVo> list = new ArrayList<>();
        OrdersVo o = null;
        ResultSet rs = null;
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            rs = exeQuery(sql, new Object[]{
                    (pageNo - 1) * pageSize,
                    pageSize
            }, conn, ps);
            while (rs.next()) {
                o = new OrdersVo();
                o.setOrderID(rs.getInt(1));
                o.setOrderNumder(rs.getString(2));
                o.setConsigneeName(rs.getString(3));
                o.setConsigneephoone(rs.getString(4));
                o.setSite(rs.getString(5));
                o.setUpmoney(rs.getDouble(6));
                o.setdName(rs.getString(7));
                o.setPaymentTime(rs.getString(8));
                o.setCreationTime(rs.getString(9));
                o.setStateName(rs.getString(10));
                o.setRemark(rs.getString(11));
                list.add(o);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            closeAll(rs, ps, conn);
        }
        return list;
    }

    /**
     * 查询总记录数
     * @param sql
     * @return
     */
    @Override
    public int selectCount(String sql) {
        ResultSet rs=null;
        Connection conn=null;
        PreparedStatement ps=null;
        try {
            rs=exeQuery(sql,null,conn,ps);
            while (rs.next()){
                return rs.getInt(1);
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }finally {
            closeAll(rs,ps,conn);
        }
        return 0;
    }

    /**
     * 根据id查询OrdersVo数据
     *
     * @param id
     * @return
     */
    @Override
    public OrdersVo selectOne(int id) {
        String sql="select a.orderID,a.order_numder,b.consigneeName,b.consigneephoone,b.site,a.upmoney,d.d_name,a.payment_time,a.creation_time,c.stateName,a.remark,a.delivery_time from orders a join site b on b.siteID=a.siteID join order_status c on c.stateID=a.stateID join distribution d on d.id=a.distribution where a.orderID=?";
        OrdersVo o = null;
        ResultSet rs = null;
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            rs = exeQuery(sql, new Object[]{
                    id
            }, conn, ps);
            while (rs.next()) {
                o = new OrdersVo();
                o.setOrderID(rs.getInt(1));
                o.setOrderNumder(rs.getString(2));
                o.setConsigneeName(rs.getString(3));
                o.setConsigneephoone(rs.getString(4));
                o.setSite(rs.getString(5));
                o.setUpmoney(rs.getDouble(6));
                o.setdName(rs.getString(7));
                o.setPaymentTime(rs.getString(8));
                o.setCreationTime(rs.getString(9));
                o.setStateName(rs.getString(10));
                o.setRemark(rs.getString(11));
                o.setDeliveryTime(rs.getString(12));
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        } finally {
            closeAll(rs, ps, conn);
        }
        return o;
    }

    /**
     * 修改发货时间
     * @param id
     * @return
     */
    @Override
    public int update(int id) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(now.format(dtf));
        String sql="UPDATE orders SET `stateID` = 5, `delivery_time` = ?  WHERE  orderID=? and `delivery_time` is NULL and `stateID`<5;";
        return exeUpdate(sql,new Object[]{
                now.format(dtf),
                id
        });
    }
}
